package cn.qin.xhangapi.controller;

import cn.qin.xhangapi.pojo.SysUsers;
import cn.qin.xhangapi.service.SysUsersService;
import cn.qin.xhangapi.util.md5.MD5Util;
import cn.qin.xhangapi.util.response.ResponseResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Objects;

@RequestMapping("/api/user")
@RestController
@CrossOrigin
public class SysUserController {
    @Resource
    private SysUsersService sysUserService;

    //判断新增用户是否已经存在
    @GetMapping("/isExist")
    public ResponseResult isExist(@RequestParam("account") String account) {
        QueryWrapper<SysUsers> wrapper = new QueryWrapper<>();
        wrapper.eq("account", account);
        SysUsers sysUsers = sysUserService.getOne(wrapper);
        if (sysUsers != null) {
            return ResponseResult.failure("用户已存在");
        }
        return ResponseResult.success("用户可用");
    }

    //分页+模糊查询获取用户账户列表
    @GetMapping("/getUserList")
    public ResponseResult getUserList(
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(name = "roleId", defaultValue = "") String roleId,
            @RequestParam(name = "account", defaultValue = "") String account
    ) {
        roleId = Objects.equals(roleId, "-1") ? "" : roleId;
        QueryWrapper<SysUsers> wrapper = new QueryWrapper<>();
        wrapper.like(!account.isEmpty(),"account", account);
        wrapper.eq(!roleId.isEmpty(),"role_id", roleId);
        wrapper.eq("u.deleted", 0);

        Page<SysUsers> page = new Page<>(pageNo, pageSize);
        sysUserService.getSysUserList(page,wrapper);

        return ResponseResult.success(page);
    }

    //根据id获取用户信息
    @GetMapping("/getUserById")
    public ResponseResult getUser(@RequestParam("id") Long id){
        return ResponseResult.success(sysUserService.getById(id));
    }

    //修改用户信息
    @PutMapping("/updateUser")
    public ResponseResult updateUser(
            @RequestParam("id") Long id,
            @RequestParam("roleId") Long roleId,
            @RequestParam("account") String account,
            @RequestParam("password") String password,
            @RequestParam("realName") String realName,
            @RequestParam("status") Integer status,
            @RequestParam("remarks") String remarks,
            @RequestParam("avatarUrl") String avatarUrl
    ){
        SysUsers sysUsers = sysUserService.getById(id);

        // 根据角色组id判断该角色组是否存在
        if(sysUsers == null) {
            return ResponseResult.failure("该系统用户不存在");
        }

        if(roleId == 1){
            status = 1;
        }

        SysUsers user = new SysUsers();
        user.setId(id);
        user.setRoleId(roleId);
        user.setAccount(account);
        user.setRealName(realName);
        user.setStatus(status);
        user.setRemarks(remarks);
        if(!password.isEmpty()){
            //使用MD5工具将密码根据盐值加密后存放到对象中
            String newPassword = MD5Util.encode(password, sysUsers.getSalts());
            user.setPassword(newPassword);
        }
        user.setAvatarUrl(avatarUrl);

        if (sysUserService.updateById(user)) {
            return ResponseResult.success("修改成功");
        }
        return ResponseResult.failure("修改失败");
    }

    //新增用户信息
    @PostMapping("/addUser")
    public ResponseResult addUser(
            @RequestParam("roleId") Long roleId,
            @RequestParam("account") String account,
            @RequestParam("password") String password,
            @RequestParam("realName") String realName,
            @RequestParam("status") Integer status,
            @RequestParam("remarks") String remarks
    ){
        if (password.isEmpty()){
            return ResponseResult.failure("密码不能为空");
        }

        //为这个用户随机生成一个盐值
        String salt = MD5Util.randSalts(8);
        //使用MD5工具将密码根据盐值加密后存放到对象中
        String newPassword = MD5Util.encode(password, salt);

        if(roleId == 1){
            status = 1;
        }

        SysUsers user = new SysUsers();
        user.setRoleId(roleId);
        user.setAccount(account);
        user.setPassword(newPassword);
        user.setRealName(realName);
        user.setStatus(status);
        user.setRemarks(remarks);
        user.setSalts(salt);

        if(isExist(account).message == "用户已存在"){
            return ResponseResult.failure("用户已存在,添加失败");
        }

        if (sysUserService.save(user)) {
            return ResponseResult.success("添加成功");
        }
        return ResponseResult.failure("添加失败");
    }

    //删除用户信息
    @DeleteMapping("/deleteUser")
    public ResponseResult deleteUser(@RequestParam("id") Long id){
        SysUsers sysUsers = sysUserService.getById(id);

        // 根据角色组id判断该角色组是否存在
        if(sysUsers == null) {
            return ResponseResult.failure("该系统用户不存在");
        }

        // 根据is_super判断该角色组是否能删除
        // is_super == 1，即最高系统用户角色组，禁止删除
        if(sysUsers.getIsSuper() == 1) {
            return ResponseResult.success("该系统用户禁止删除");
        }

        if(sysUserService.removeById(id)){
            return ResponseResult.success("删除成功");
        }
        return ResponseResult.failure("删除失败");
    }
}
